/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import abstractmodel.IAccount;
import abstractmodel.IInfo;
import abstractmodel.IUser;
import abstractmodel.StatisticsType;
import java.util.ArrayList;

/**
 *
 * @author fightfxj
 */
public class Book implements abstractmodel.IBook {

    private BookInfo bookInfo;
    private ArrayList<IAccount> accounts;
    private IUser ownerUser;

    @Override
    public IUser getOwnerUser() {
        return ownerUser;
    }

    public void setOwnerUser(User ownerUser) {
        this.ownerUser = ownerUser;
    }

    public Book() {
        bookInfo = new BookInfo();
        accounts = new ArrayList<IAccount>();
    }

    @Override
    public IInfo getBookInfo() {
        return bookInfo;
    }

    @Override
    public boolean setBookInfo(IInfo info) {
        bookInfo = (BookInfo) info;
        return true;
    }

    @Override
    public boolean addNewAccount(IAccount account) {
        ((Account)account).setOwnerBook(this);
        accounts.add(account);
        if (ownerUser != null)
            ownerUser.commit();
        return true;
    }

    @Override
    public boolean deleteAccount(IAccount account) {
        accounts.remove(account);
        if (ownerUser != null)
            ownerUser.commit();
        return true;
    }

    @Override
    public ArrayList<IAccount> getAccounts() {
        return accounts;
    }

    @Override
    public StatisticsInfo getStatistics(StatisticsType type) {
        StatisticsInfo sinfo = new StatisticsInfo();
        System.out.println("帐户数量：" + accounts.size());
        for (IAccount a : accounts) {
            System.out.println(a.getAccountInfo().getValue("帐户名称").getString() + " " + new Double(a.getTotalLeftMoney()));
            sinfo.add(a.getAccountInfo().getValue("帐户名称").getString(), new Double(a.getTotalLeftMoney()));
        }
        sinfo.setType(type);
        return sinfo;
    }

    @Override
    public double getTotalLeftMoney() {
        double total = 0.0;
        for (IAccount a : accounts) {
            total += a.getTotalLeftMoney();
        }
        return total;
    }

    /**
     * 打印账簿的所有相关信息，供调试用
     */
    public void printAllBookInfo() {
        //titles = new String[]{"账簿名称", "创建时间", "备注"}
        System.out.println("Titles : ");
        for (int i = 0; i < bookInfo.getTitles().length; i++) {
            System.out.print(bookInfo.getTitles()[i] + " ");
        }
        System.out.println();
        System.out.println("账簿名称：" + bookInfo.getValue("账簿名称").getString());
        System.out.println("创建时间：" + bookInfo.getValue("创建时间").getString());
        System.out.println("备注：" + bookInfo.getValue("备注").getString());
        System.out.println("ID：" + bookInfo.getValue("id").getString());
    }

    @Override
    public ArrayList<IAccount> getAccountsLeftMoneyMoreThan(double d) {
        ArrayList<IAccount> accountsWant = new ArrayList<IAccount>();
        for (IAccount a : this.getAccounts()) {
            if (a.getTotalLeftMoney() > d)
                accountsWant.add(a);
        }
        return accountsWant;
    }

}
